﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class admin_Catalog : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectAASP_KFCConnectionString"].ConnectionString);
    SqlCommand com = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            loadCategoryData();
        }
    }

    private void loadCategoryData()
    {
        com.Connection = con;

        com.CommandText = "SELECT * FROM Category";
        da.SelectCommand = com;
        da.Fill(ds);

        Session["CategoryList"] = ds.Tables[0].DefaultView;
        GridView1.DataSource = Session["CategoryList"];
        GridView1.DataBind();
        ds.Clear();

    }

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridView1.PageIndex = e.NewSelectedIndex;
        //GridView1.PageIndex= e.NewPageIndex;
        GridView1.DataSource = Session["CategoryList"];
        GridView1.DataBind();
    }
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        
    }
    protected void Add_Click(object sender, EventArgs e)
    {
        try
        {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["lab4ConnectionString"].ConnectionString;
            com.Connection = con;
            com.CommandText = "INSERT INTO MonHoc VALUES( @CatagoryName, @CatagoryImage) ";
            com.Parameters.AddWithValue("@CatagoryName", TextBoxNameCatalog.Text);
            com.Parameters.AddWithValue("@CatagoryImage", FileUpload1.FileName);

            con.Open();
            com.ExecuteNonQuery();
            Response.Write("insert Thang Cong.");
        }
        catch
        {
            Response.Write("Loi insert.");
        }
        finally
        {

            con.Close();
            loadCategoryData();

        }
    }
}